package com.yyc.system.controller;

import com.alibaba.fastjson.JSON;
import com.yyc.core.Result;
import com.yyc.core.ResultGenerator;
import com.yyc.system.model.TClassUser;
import com.yyc.system.model.TUser;
import com.yyc.system.service.ITClassUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyc.system.service.ITUserService;
import com.yyc.utils.CommonUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
* Created by yyc on 2018/12/26.
*/
@RestController
@RequestMapping("/t/class/user")
public class TClassUserController {
    @Autowired
    private ITClassUserService tClassUserService;
    @Autowired
    private ITUserService tUserService;

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map) {
        TUser user = JSON.parseObject(JSON.toJSONString(map.get("user")), TUser.class);
        tUserService.save(user);
        String classId = CommonUtils.getMapValue(map, "classId");
        TClassUser tClassUser = new TClassUser();
        tClassUser.setUserId(user.getId());
        tClassUser.setClassId(Integer.valueOf(classId));
        tClassUserService.save(tClassUser);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        tClassUserService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TClassUser tClassUser) {
        tClassUserService.update(tClassUser);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        TClassUser tClassUser = tClassUserService.findById(id);
        return ResultGenerator.genSuccessResult(tClassUser);
    }

    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TClassUser> list = tClassUserService.findAll();
        PageInfo<TClassUser> pageInfo = new PageInfo<TClassUser>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    
    //****************************************************************************
    //查询班级同学列表
    @RequestMapping("/queryClassStudent")
    public Result queryClassStudent(Integer classId) {
    	List<TClassUser> list = tClassUserService.findClassStudent(classId);
    	return ResultGenerator.genSuccessResult(list);
    }

    //查询学生列表
    @RequestMapping("/queryStudentList")
    public Result queryStudentList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TClassUser> list = tClassUserService.findStudentList();
        PageInfo<TClassUser> pageInfo = new PageInfo<TClassUser>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
